## Created by EmpowerStats @ Thu, 20 Feb 25 19:00:59 +0800## 
#******************** Regarding ALL Following R Code   ******************** 
#*** COPYRIGHT (c) 2010, 2021 X&Y Solutions, ALL RIGHT RESERVED *********** 
#******************* www.EmpowerStats.com ********************************* 
#************************************************************************** 
Sys.setlocale(category = 'LC_ALL', locale = 'English_United States.1252'); 
.libPaths(file.path(R.home(),'library')); 
library(doBy); 
options(timeout=600); 
library(plotrix); 
library(stringi); 
library(stringr); 
library(survival); 
library(rms); 
library(nnet); 
library(car); 
library(mgcv); 
pdfwd<-6; pdfht<-6; 
load('/Users/liukangning/Desktop/EmpowerROS/Analysis/cd/datamergeall.Rdata'); 
if (length(which(ls()=='EmpowerStatsR'))==0) EmpowerStatsR<-get(ls()[1]); 
names(EmpowerStatsR)<-toupper(names(EmpowerStatsR)); 
originalVNAME<-names(EmpowerStatsR); 
ofname<-'cd_8_tbl'; 
vname<-c(NA,'EDUCATION','EDUCATION.1','EDUCATION.2','EDUCATION.3','EDUCATION.4','EDUCATION.5','MARITAL','MARITAL.1','MARITAL.2','MARITAL.3','MARITAL.4','MARITAL.5','MARITAL.6','PIR','GENDER','GENDER.1','GENDER.2','AGE','RACE','RACE.1','RACE.2','RACE.3','RACE.4','RACE.6','RACE.7','BMI','BP','BP.1','BP.2','CORONARY','CORONARY.1','CORONARY.2','STROKE','STROKE.1','STROKE.2','DPQ','AFT','CERAD','DSST')[-1]; 
vlabel<-c(NA,'EDUCATION','  Less than 9th grade','  9-11th grade (Includes 12th grade with no diploma)','  High school graduate/GED or equivalent','  Some college or AA degree','  College graduate or above','MARITAL','  Married','  Widowed','  Divorced','  Separated','  Never married','  Living with partner','PIR','GENDER','  Male','  Female','AGE','RACE','  Mexican American','  Other Hispanic','  Non-Hispanic White','  Non-Hispanic Black','  Non-Hispanic Asian','  Other Race - Including Multi-Racial','BMI','BP','  1','  2','CORONARY','  1','  2','STROKE','  1','  2','DPQ','AFT','CERAD','DSST')[-1]; 
varused4this <- c('EDUCATION','MARITAL','PIR','GENDER','AGE','RACE','BMI','BP','CORONARY','STROKE','DPQ','AFT','CERAD','DSST'); 
pkgs<-c('gdata','geepack','mgcv'); 
for (g in pkgs) {  
if (!(g %in% rownames(installed.packages()))) install.packages(g,repos='https://cloud.r-project.org'); 
}
library(gdata); 
library(geepack); 
library(mgcv); 
WD <- EmpowerStatsR; rm(EmpowerStatsR); gc(); 
title<-'分层分析'; 
wd.subset=''; 
weights.var<- NA; 
yvname<-c('DSST','AFT','CERAD'); 
ydist<-c('gaussian','gaussian','gaussian'); 
ylink<-c('identity','identity','identity'); 
ylv<-c(0,0,0); 
xvname<-c('EDUCATION','MARITAL','AGE','RACE','BMI','GENDER'); 
sxf<-c(0,0,2,0,3,0); 
xlv<-c(5,6,0,6,0,2); 
svname<-c('PIR','BP','CORONARY','STROKE'); 
sdf<-c(0,0,0,0); 
slv<-c(0,2,2,2); 
avname<-c('DPQ'); 
alv<-c(0); 
cox<- 0; 
timevar<- NA; 
vname.start<- NA; 
subjvname<- NA; 
gee.TYPE<-NA; 
bvar<- NA; 
par1<-10; 
prn<-1; 
dec<-2; 
##R package## gdata geepack mgcv ##R package##;
pvformat<-function(p,dec) {
  pp <- sprintf(paste("%.",dec,"f",sep=""),as.numeric(p))
  if (is.matrix(p)) {pp<-matrix(pp, nrow=nrow(p)); colnames(pp)<-colnames(p);rownames(pp)<-rownames(p);}
  lw <- paste("<",substr("0.00000000000",1,dec+1),"1",sep="");
  pp[as.numeric(p)<(1/10^dec)]<-lw
  return(pp)
}
numfmt<-function(p,dec) {
  if (is.list(p)) p<-as.matrix(p)
  pp <- sprintf(paste("%.",dec,"f",sep=""),as.numeric(p))
  if (is.matrix(p)) {pp<-matrix(pp, nrow=nrow(p));colnames(pp)<-colnames(p);rownames(pp)<-rownames(p);}
  pp[as.numeric(p)>10000000]<- "inf."
  pp[is.na(p) | gsub(" ","",p)==""]<- ""
  pp[p=="-Inf"]<-"-Inf"
  pp[p=="Inf"]<-"Inf"
  return(pp)
}
mat2htmltable<-function(mat) {
  t1<- apply(mat,1,function(z) paste(z,collapse="</td><td>"))
  t2<- paste("<tr><td>",t1,"</td></tr>")
  return(paste(t2,collapse=" "))
}
setgam<-function(fml,yi) {
  if (ydist[yi]=="") ydist[yi]<-"gaussian"
  if (ydist[yi]=="exact") ydist[yi]<-"binomial"
  if (ydist[yi]=="breslow") ydist[yi]<-"binomial"
  if (ydist[yi]=="gaussian") mdl<-try(gam(formula(fml),weights=wdtmp$weights,data=wdtmp, family=gaussian(link="identity")))
  if (ydist[yi]=="binomial") mdl<-try(gam(formula(fml),weights=wdtmp$weights,data=wdtmp, family=binomial(link="logit")))
  if (ydist[yi]=="poisson") mdl<-try(gam(formula(fml),weights=wdtmp$weights,data=wdtmp, family=poisson(link="log")))
  if (ydist[yi]=="gamma") mdl<-try(gam(formula(fml),weights=wdtmp$weights,data=wdtmp, family=Gamma(link="inverse")))
  if (ydist[yi]=="negbin") mdl<-try(gam(formula(fml),weights=wdtmp$weights,data=wdtmp, family=negbin(c(1,10), link="log")))
  return(mdl)
}
setgee<-function(fml,yi) {
  if (ydist[yi]=="") ydist[yi]<-"gaussian"
  if (ydist[yi]=="exact") ydist[yi]<-"binomial"
  if (ydist[yi]=="breslow") ydist[yi]<-"binomial"
  if (ydist[yi]=="gaussian") md<-try(geeglm(formula(fml),id=wdtmp[,subjvname],corstr=gee.TYPE,family="gaussian",weights=wdtmp$weights,data=wdtmp))
  if (ydist[yi]=="binomial") md<-try(geeglm(formula(fml),id=wdtmp[,subjvname],corstr=gee.TYPE,family="binomial",weights=wdtmp$weights,data=wdtmp))
  if (ydist[yi]=="poisson") md<-try(geeglm(formula(fml),id=wdtmp[,subjvname],corstr=gee.TYPE,family="poisson",weights=wdtmp$weights,data=wdtmp))
  if (ydist[yi]=="gamma") md<-try(geeglm(formula(fml),id=wdtmp[,subjvname],corstr=gee.TYPE,family="Gamma",weights=wdtmp$weights,data=wdtmp))
  if (ydist[yi]=="negbin") md<-try(geeglm.nb(formula(fml),id=wdtmp[,subjvname],corstr=gee.TYPE,weights=wdtmp$weights,data=wdtmp))
  return(md)
}
setglm<-function(fml,yi) {
  if (ydist[yi]=="") ydist[yi]<-"gaussian"
  if (ydist[yi]=="exact") ydist[yi]<-"binomial"
  if (ydist[yi]=="breslow") ydist[yi]<-"binomial"
  if (ydist[yi]=="gaussian") md<-try(glm(formula(fml),family="gaussian",weights=wdtmp$weights,data=wdtmp))
  if (ydist[yi]=="binomial") md<-try(glm(formula(fml),family="binomial",weights=wdtmp$weights,data=wdtmp))
  if (ydist[yi]=="poisson") md<-try(glm(formula(fml),family="poisson",weights=wdtmp$weights,data=wdtmp))
  if (ydist[yi]=="gamma") md<-try(glm(formula(fml),family="Gamma",weights=wdtmp$weights,data=wdtmp))
  if (ydist[yi]=="negbin") md<-try(glm.nb(formula(fml),weights=wdtmp$weights,data=wdtmp))
  return(md)
}
mdl2oo<-function(mdl, xxname, opt) {
  if (is.na(mdl[[1]][1])) return(list(rep("",times=length(xxname)),""))
  if (substr(mdl[[1]][1],1,5)=="Error") return(list(rep("",times=length(xxname)),""))
  decp<-dec+2; if (decp>4) decp<-4;
  gs<-summary(mdl); print(mdl$formula); print(gs)
  if (opt=="gam") {gsparm <- gs$p.table;tmpn<-gs$n;
  } else {gsparm <- gs$coefficients;tmpn <- sum(gs$df[c(1,2)]);}
  gsp<-gsparm[match(xxname,rownames(gsparm)),]
  if (length(xxname)==1) {beta<-gsp[1]; se<-gsp[2]; pv<-gsp[4]; 
  } else {beta<-gsp[,1]; se<-gsp[,2]; pv<-gsp[,4]; }
  ci1<- beta-1.96*se; ci2<- beta+1.96*se
  pvx<-substr(rep("****",length(pv)),1,(pv<=0.05)+(pv<=0.01)+(pv<=0.001)) 
  if (colprn==3) {pvv<-pvx;} else {pvv<-pvformat(pv,decp);}
  if ((colprn!=2) & (gs$family[[2]]=="log" | gs$family[[2]]=="logit")) {
    o1<-paste(numfmt(exp(beta),dec)," (",numfmt(exp(ci1),dec),", ",numfmt(exp(ci2),dec),")",sep="")
  } else {
    if (colprn<3) {o1<-paste(numfmt(beta,dec), " (",numfmt(ci1,dec),", ",numfmt(ci2,dec),")",sep="")
    } else {o1<-paste(numfmt(beta,dec), "+",numfmt(se,dec),sep="");}
  }   
  o1<-paste(o1,pvv)
  if (length(xxname)>1) {
    o1[1]<-""; o1[2]<-"0";
    if (gs$family[[2]]=="log" | gs$family[[2]]=="logit") o1[2]<-"1.0"
  }
  return(list(o1,tmpn))
}
recodevar <- function (var,oldcode,newcode) {
  tmp.v <- var
  nc.tmp <- length(oldcode)
  for (i in (1:nc.tmp)) {tmp.v[(var==oldcode[i])]=newcode[i]}
  if (is.factor(tmp.v)) {tmp.v1<-as.numeric(as.character(tmp.v))} else {tmp.v1<-as.numeric(tmp.v)}
  rm(tmp.v);  return(tmp.v1)
} 
rankvar <- function(var, num) {
  qprobs <- 1/num
  if (num>2) {for (i in (2:(num-1))) {qprobs <- c(qprobs, 1/num * i) } } 
  outvar <- rep(0, times=length(var))
  outvar[is.na(var)] <- NA
  cutpoints <- quantile(var,probs=qprobs, na.rm=TRUE)
  for (k in (1:length(cutpoints))) { outvar[var>=cutpoints[k]] <- k; }
  return(outvar)
}
removeNA<-function(i,j,k,wdf) {
 vvv<-c(yvname[i],avname[j],xvname[k],subjvname,bvar,vname.start,timevar); 
 vvv<-vvv[!is.na(vvv)]; vvv<-vvv[vvv>" "]
 tmp<-is.na(wdf[,vvv]); 
 return(wdf[apply(tmp,1,sum)==0,])
}
varstats<-function(k,j,wdtmp0) {
  vk<-xvname[k]; klv<-xxlvl_[[k]];nklv<-length(klv);
  if (!is.na(j)) {vj<-avname[j]; jlv<-aalvl_[[j]];njlv<-length(jlv);} else njlv<-0;
  if (njlv<=2) {
    a<-table(wdtmp0[,vk])
    return(cbind(xxlbl_[[k]],c(" ",a[match(klv,names(a))])))
  } else {
    a<-table(wdtmp0[,vk],wdtmp0[,vj])
    a<-a[match(klv,rownames(a)),match(jlv,colnames(a))]
    oon<-c(" ",matrix(t(cbind(" ",a)),ncol=1))
    t1<-matrix(rep(paste("&nbsp&nbsp",aalbl_[[j]][-1]),nklv),ncol=nklv)
    return(cbind(c(xb[k],matrix(rbind(xxlbl[[k]],t1),ncol=1)),oon))
  }
}
if (!is.na(weights.var)) {weights<-WD[,weights.var];} else {weights<-1;}
WD<-cbind(WD,weights);
vlabelN<-(substr(vlabel,1,1)==" ");
vlabelZ<-vlabel[vlabelN];vlabelV<-vlabel[!vlabelN]
vnameV<-vname[!vlabelN];vnameZ<-vname[vlabelN];
w<-c("<!DOCTYPE html><html lang='zh'><head><meta charset='utf-8'></head><body>")
w<-c(w,paste("<h2>", title, "</h2>"))
if (length(svname)>0) { 
  if (sum((sdf=="s" | sdf=="S") & slv>0)>0) w<-c(w,"</br>Spline smoothing only applies for continuous variables")
  sdf[slv>0]<-"";
  if (!is.na(subjvname) & sum(sdf=="s" | sdf=="S")>0) {
    sdf<-rep(0,length(svname));
    w<-c(w,"</br>Generalized estimate equation could not be used with spline smoothing terms")
  }
}
sxf<-as.numeric(sxf);sxf[is.na(sxf)]<-0;
if (sum(xlv==0 & sxf==0)>0) w<-c(w,"</br>Stratified variables should be categorical")
if (sum(sxf>1 & xlv>0)>0) w<-c(w,"Categorizing only applies to continuous variables");
allvname<-c(yvname,xvname,svname,bvar,avname,subjvname,vname.start,timevar,"weights"); 
allvname<-allvname[!is.na(allvname)]
WD<-WD[,allvname];
if (!is.na(subjvname)) WD<-WD[order(WD[,subjvname]),]
sxf[xlv>0]<-0
sxf[xlv==0 & sxf==0]<-3
nx<-length(xvname)
if (sum(sxf>1 & xlv==0)>0) {
     t.xname<-NA;t.xlv<-NA; 
     for (i in 1:nx) {
       if (sxf[i]>1 & xlv[i]==0) {
          tmp.Xi<- rankvar(WD[,xvname[i]],sxf[i])
          tmp.newcode <- tapply(WD[,xvname[i]],tmp.Xi,function(z) median(z,na.rm=TRUE))
          tmp.low <- tapply(WD[,xvname[i]],tmp.Xi,function(z) min(z,na.rm=TRUE))
          tmp.upp <- tapply(WD[,xvname[i]],tmp.Xi,function(z) max(z,na.rm=TRUE))
          tmp.Xi2<- recodevar(tmp.Xi,(1:sxf[i])-1,tmp.newcode)
          tmp.Xi<-cbind(tmp.Xi,tmp.Xi2)
          tmp.NM<-paste(xvname[i],c("grp","grp.cont"),sep=".")
          colnames(tmp.Xi)<-tmp.NM
          WD<-cbind(WD,tmp.Xi)
          t.xname<-c(t.xname,tmp.NM)
          t.xlv<-c(t.xlv,sxf[i],0)
          vnameV<-c(vnameV,tmp.NM)
          vlabelV<-c(vlabelV,paste(vlabelV[vnameV==xvname[i]],c("group","group trend")))
          vnameZ<-c(vnameZ,paste(tmp.NM[1],(1:sxf[i])-1,sep="."))
          vlabelZ<-c(vlabelZ,paste(tmp.low,"-",tmp.upp))
       } else {
          t.xname<-c(t.xname,xvname[i]); t.xlv<-c(t.xlv,xlv[i])
       }
     }
     xvname<-t.xname[-1]; xlv<-t.xlv[-1];
}
xvname<-xvname[xlv>0]; xlv<-xlv[xlv>0]; nx<-length(xvname)
xxlbl_<-list(NA); xxlvl_<-list(NA); xxlbl<-list(NA); 
xb<-vlabelV[match(xvname,vnameV)]; xb[is.na(xb)]<-xvname[is.na(xb)]
for (j in 1:nx) {
  tmp<-levels(factor(WD[,xvname[j]])); ntmp<-length(tmp)
  xxlvl_[[j+1]]<-tmp;
  tmp<-paste(xvname[j],".",tmp,sep="")
  xxlbl_[[j+1]]<-paste(c("",rep("&nbsp&nbsp",ntmp)),c(xb[j],vlabelZ[match(tmp,vnameZ)]))
  xxlbl[[j+1]]<-paste(xb[j],"=",vlabelZ[match(tmp,vnameZ)])
}
xxlbl_<-xxlbl_[-1]; xxlvl_<-xxlvl_[-1]; xxlbl<-xxlbl[-1]
avname<-c(avname[alv<=2],avname[alv>2])
alv<-c(alv[alv<=2],alv[alv>2]); alv[alv<=2]<-0;
na<-length(avname); na0<-sum(alv<=2); na1<-sum(alv>2);
ab<-vlabelV[match(avname,vnameV)]
avname_<-avname
avname_[alv>2]<-paste("factor(",avname[alv>2],")",sep="")
aaname_<-list(NA); aalbl_<-list(NA); aalvl_<-list(NA)
for (j in (1:na)) {
  if (alv[j]==0) {
    aaname_[[j+1]]<-avname[j];aalbl_[[j+1]]<-ab[j];aalvl_[[j+1]]<-0
  } else {
    aalvl_[[j+1]]<-levels(factor(WD[,avname[j]]))
    tmp<-paste(avname[j],".",aalvl_[[j+1]],sep="")
    aalbl_[[j+1]]<-c(ab[j],vlabelZ[match(tmp,vnameZ)])
    aalbl_[[j+1]]<-paste(c("",rep("&nbsp&nbsp",length(aalbl_[[j+1]])-1)),aalbl_[[j+1]])
    aaname_[[j+1]]<-c(avname[j],paste("factor(",avname[j],")",aalvl_[[j+1]],sep=""))
  }
}
aaname_<-aaname_[-1]; aalbl_<-aalbl_[-1]; aalvl_<-aalvl_[-1]
ns=0; svb=""; smoothsv<-0; fmladj<-"";
if (length(svname)>0) {
  svb<-vlabelV[match(svname,vnameV)];
  svname_ <- svname 
  smoothsvi<-((sdf=="s" | sdf=="S") & slv==0)
  smoothsv<-sum(smoothsvi)
  svname_[smoothsvi]<-paste("s(",svname[smoothsvi],")",sep="")
  svb1<-svb
  svb1[smoothsvi]<-paste(svb[smoothsvi],"(Smooth)",sep="")
  svname_[slv>0]<-paste("factor(",svname[slv>0],")",sep="")
  fmladj<-paste("+",paste(svname_,collapse="+"))
}
ny<-length(yvname);
yb<-vlabelV[match(yvname,vnameV)]; yb[is.na(yb)]<-yvname[is.na(yb)]
if (is.na(bvar)) {
  blvb<-"Total"; blvb_<-"Total"
} else {
  blv<-levels(factor(WD[,bvar])); nblv<-length(blv)+1
  blvb_<-vlabelZ[match(paste(bvar,".",blv,sep=""),vnameZ)]; blvb_[is.na(blvb_)]<-blv[is.na(blvb_)];
  blvb<-c(paste(vlabelV[vnameV==bvar],blvb_,sep="="),"Total");
  blvb_<-c(blvb_,"Total")
  WD<-WD[!is.na(WD[,bvar]),]
}
opt<-ifelse(!is.na(subjvname), "gee", ifelse(smoothsv>0, "gam", "glm"));
colprn<-prn;
if (!is.na(bvar)) {prn<-"S";
} else if (na==1 & alv[1]>2) {prn<-"CX";
} else if (ny>1 & ny<5) {prn<-"Y";
} else if (na>1 & na<5 & sum(alv>2)==0) {prn<-"X";
} else {prn<-"Y";}
if (par1 < 5 | par1 == "") par1 = 5
minstsize = par1 + ns
sink(paste(ofname,".lst",sep=""))
if (prn=="Y") {
  tt<-rep(NA,ny+2); nn<-c("Exposure","Sub-group",yb)
  for (j in 1:na) {
     tt<-rbind(tt,c(paste("X=",ab[j]),rep(" ",ny+1)),c("","N",yb));
     for (k in 1:nx) {
       wdtmp0<-removeNA(NA,j,k,WD)
       colk<-varstats(k,j,wdtmp0);
       nlvl<-length(xxlvl_[[k]])
       nnk<-cbind(ab[j],xxlbl[[k]])
       for (i in 1:ny) {
          wdtmp0<-removeNA(i,j,k,WD)
          fml<-paste(yvname[i],"~",avname_[j],fmladj)
          tmpc<-""; tmpnn<-"";
          for (v in 1:nlvl) {
            print(xxlbl[[k]][v]);
            wdtmp<-wdtmp0[wdtmp0[,xvname[k]]==xxlvl_[[k]][v],]
            tmpn<-nrow(wdtmp); tmpnn<-c(tmpnn,tmpn)
            if (tmpn>minstsize) {
              if (opt=="gam") tmp.mdl<-setgam(fml,i)
              if (opt=="gee") tmp.mdl<-setgee(fml,i)
              if (opt=="glm") tmp.mdl<-setglm(fml,i)
              tmpooi<-mdl2oo(tmp.mdl,aaname_[[j]],opt)
              tmpc<-c(tmpc,tmpooi[[1]]);
            } else {tmpc<-c(tmpc,rep(" ",length(aalbl_[[j]])));}
          }
          colk<-cbind(colk,tmpc); nnk<-cbind(nnk,tmpnn[-1])
       }
       tt<-rbind(tt,colk); nn<-rbind(nn,nnk)
     }
  }
}

if (prn=="X") {
  tt<-rep(NA,na+2); nn<-c("Outcome","Sub-group",ab)
  for (i in 1:ny) {
     tt<-rbind(tt,c(paste("Y=",yb[i]),rep(" ",na+1)),c("","N",ab));
     for (k in 1:nx) {
       wdtmp0<-removeNA(i,NA,k,WD)
       colk<-varstats(k,NA,wdtmp0);
       nlvl<-length(xxlvl_[[k]])
       nnk<-cbind(yb[i],xxlbl[[k]])
       for (j in 1:na) {
          wdtmp0<-removeNA(i,j,k,WD)
          fml<-paste(yvname[i],"~",avname_[j],fmladj)
          tmpc<-""; tmpnn<-"";
          for (v in 1:nlvl) {
            print(xxlbl[[k]][v]);
            wdtmp<-wdtmp0[wdtmp0[,xvname[k]]==xxlvl_[[k]][v],]
            tmpn<-nrow(wdtmp); tmpnn<-c(tmpnn,tmpn)
            if (tmpn>minstsize) {
              if (opt=="gam") tmp.mdl<-setgam(fml,i)
              if (opt=="gee") tmp.mdl<-setgee(fml,i)
              if (opt=="glm") tmp.mdl<-setglm(fml,i)
              tmpooi<-mdl2oo(tmp.mdl,aaname_[[j]],opt)
              tmpc<-c(tmpc,tmpooi[[1]])
            } else {tmpc<-c(tmpc," ");}
          }
          colk<-cbind(colk,tmpc); nnk<-cbind(nnk,tmpnn[-1])
       }
       tt<-rbind(tt,colk); nn<-rbind(nn,nnk)
     }
  }
}
if (prn=="CX") {
  nlv<-length(aalvl_[[1]]); tt<-rep(NA,nlv+2); nn<-c("Outcome","Sub-group",ab[1])
  for (i in 1:ny) {
     tt<-rbind(tt,c(paste("Y=",yb[i]),rep(" ",nlv+1)),c("","N",aalbl_[[1]][-1]));
     for (k in 1:nx) {
       wdtmp0<-removeNA(i,1,k,WD)
       colk<-varstats(k,NA,wdtmp0);
       nlvl<-length(xxlvl_[[k]])
       nnk<-cbind(yb[i],xxlbl[[k]])
       fml<-paste(yvname[i],"~",avname_[1],fmladj,sep="")
       tmpc<-rep("",nlv); tmpnn<-""
       for (v in 1:nlvl) {
          print(xxlbl[[k]][v]);
          wdtmp<-wdtmp0[wdtmp0[,xvname[k]]==xxlvl_[[k]][v],]
          tmpn<-nrow(wdtmp); tmpnn<-c(tmpnn,tmpn)
          if (tmpn>minstsize) {
            if (opt=="gam") tmp.mdl<-setgam(fml,i)
            if (opt=="gee") tmp.mdl<-setgee(fml,i)
            if (opt=="glm") tmp.mdl<-setglm(fml,i)
            tmpooi<-mdl2oo(tmp.mdl,aaname_[[1]],opt)
            tmpc<-rbind(tmpc,tmpooi[[1]][-1])
          } else {
            tmpc<-rbind(tmpc,rep(" ",nlv))
          }
       }
       colk<-cbind(colk,tmpc); nnk<-cbind(nnk,tmpnn[-1])
       tt<-rbind(tt,colk); nn<-rbind(nn,nnk)
     }
  }
}
if (prn=="S") {
  tt<-rep(NA,nblv+2); nn<-c("Outcome","Exposure","Sub-group",blvb)
  for (i in 1:ny) {
     tt<-rbind(tt,c(paste("Y=",yb[i]),rep(" ",nblv+1)));
     for (j in 1:na) {
       tt<-rbind(tt,c(paste("X=",ab[j]),rep(" ",nblv+1)),c("","Total N",blvb));
       for (k in 1:nx) {
          wdtmp0<-removeNA(i,j,k,WD)
          colk<-varstats(k,j,wdtmp0);
          nlvl<-length(xxlvl_[[k]])
          nnk<-cbind(yb[i],ab[j],xxlbl[[k]])
          fml<-paste(yvname[i],"~",avname_[j],fmladj)
          for (b in 1:nblv) {
            if (b<nblv) {wdtmp1<-wdtmp0[wdtmp0[,bvar]==blv[b],];} else {wdtmp1<-wdtmp0;}
            tmpc<-""; tmpnn<-""
            for (v in 1:nlvl) {
              print(paste(blvb[b],xxlbl[[k]][v],sep="; "));
              wdtmp<-wdtmp1[wdtmp1[,xvname[k]]==xxlvl_[[k]][v],]
              tmpn<-nrow(wdtmp); tmpnn<-c(tmpnn,tmpn)
              if (tmpn>minstsize) {
                if (opt=="gam") tmp.mdl<-setgam(fml,i)
                if (opt=="gee") tmp.mdl<-setgee(fml,i)
                if (opt=="glm") tmp.mdl<-setglm(fml,i)
                tmpooi<-mdl2oo(tmp.mdl,aaname_[[j]],opt)
                tmpc<-c(tmpc,tmpooi[[1]]);
              } else {tmpc<-c(tmpc,rep(" ",length(aalbl_[[j]])));}
            }
            colk<-cbind(colk,tmpc); nnk<-cbind(nnk,tmpnn[-1])
          }
          tt<-rbind(tt,colk); nn<-rbind(nn,nnk)
       }
     }
  }
}
tt<-tt[-1,]
sink()
w<-c(w,"</br><table border=3>", mat2htmltable(tt), "</table>")
prnopt<-c("β (95%CI) Pvalue / OR (95%CI) Pvalue", "β (95%CI) Pvalue", "β+se / OR (95%CI) *P<0.05 **P<0.01 ***P<0.001")
 

w<-c(w,"</br>表中数据：", prnopt[colprn])
w<-c(w,paste(c("</br>结果变量:",paste(yb,collapse="; ")),collapse=" "))
w<-c(w,paste(c("</br>暴露变量:",paste(ab,collapse="; ")),collapse=" "))
if (length(svname)==0) svb1<-"None";
w<-c(w,paste(c("</br>调整变量:",paste(svb1,collapse="; ")),collapse=" "))
if (smoothsv>0) w<-c(w,". Generalized additive models were applied")
if (opt=="gee") w<-c(w, paste("</br>Generalized estimate equation were used, subject ID=", subjvname, "(", gee.TYPE,")",sep=""))
w<-c(w,"</br></br>各模型所用的样本量</br><table border=3>", mat2htmltable(nn), "</table>")
w<-c(w,wd.subset)
w<-c(w,paste("</br>此表用易侕统计软件(www.empowerstats.com) 和R软件生成，生成日期：",Sys.Date()))
w<-c(w,"</body></html>")
fileConn<-file(paste(ofname,".htm",sep="")); writeLines(w, fileConn)